home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 August (Alt) / CHIP 2005-08.1.iso / program / guvenlik / syslinux-3.07.exe / menu / complex.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-14  |  8.3 KB  |  263 lines

  1. /* -*- c -*- ------------------------------------------------------------- *
  2.  *   
  3.  *   Copyright 2004 Murali Krishnan Ganapathy - All Rights Reserved
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
  8.  *   Boston MA 02111-1307, USA; either version 2 of the License, or
  9.  *   (at your option) any later version; incorporated herein by reference.
  10.  *
  11.  * ----------------------------------------------------------------------- */
  12.  
  13. #ifndef NULL
  14. #define NULL ((void *) 0)
  15. #endif
  16.  
  17. #include "menu.h"
  18. #include "biosio.h"
  19. #include "string.h"
  20. #include "syslinux.h"
  21.  
  22. /* Global variables */
  23. char infoline[160];
  24.  
  25. // Different network options
  26. static char nonet[] = "<n>etwork [none]";
  27. static char dhcpnet[]="<n>etwork [dhcp]";
  28. static char statnet[]="<n>etwork [static]";
  29.  
  30. struct {
  31.     unsigned int baseurl : 1; // Do we need to specify by url
  32.     unsigned int mountcd : 1; // Should we mount the cd
  33.     unsigned int winrep  : 1; // Want to repair windows?
  34.     unsigned int linrep  : 1; // Want to repair linux?
  35. } flags;
  36.  
  37. t_menuitem *baseurl,*mountcd,*network,*runprep,*winrep,*linrep;
  38. // Some menu options
  39. t_menuitem * stat,*dhcp,*none;
  40. // all the menus we are going to declare
  41. char TESTING,RESCUE,MAIN,PREP,NETMENU;
  42.  
  43. /* End globals */
  44.  
  45.  
  46. TIMEOUTCODE ontimeout()
  47. {
  48.   beep();
  49.   return CODE_WAIT;
  50. }
  51.  
  52.  
  53. #define INFLINE 22
  54.  
  55. void msys_handler(t_menusystem *ms, t_menuitem *mi)
  56. {
  57.     char nc;
  58.     void *v;
  59.     nc = getnumcols(); // Get number of columns
  60.  
  61.     if (mi->parindex != PREP) // If we are not in the PREP MENU
  62.     {
  63.         gotoxy(INFLINE,0,ms->menupage);
  64.         cprint(' ',0x07,nc,ms->menupage);
  65.         return;
  66.     }
  67.     strcpy (infoline," ");
  68.     if (flags.baseurl) strcat(infoline,"baseurl=http://192.168.11.12/gui ");
  69.     if (flags.mountcd) strcat(infoline,"mountcd=yes ");
  70.     v = (void *)network->data;
  71.     if (v!=NULL) // Some network option specified
  72.       {
  73.     strcat(infoline,"network=");
  74.     strcat(infoline,(char *)(((t_menuitem *)v)->data));
  75.       }
  76.     if (flags.winrep) strcat(infoline,"repair=win ");
  77.     if (flags.linrep) strcat(infoline,"repair=lin ");
  78.  
  79.     gotoxy(INFLINE,0,ms->menupage);
  80.     cprint(' ',0x07,nc,ms->menupage);
  81.     gotoxy(INFLINE+1,0,ms->menupage);
  82.     cprint(' ',0x07,nc,ms->menupage);
  83.     gotoxy(INFLINE,0,ms->menupage);
  84.     csprint("Kernel Arguments:",0x07);
  85.     gotoxy(INFLINE,17,ms->menupage);
  86.     csprint(infoline,0x07);
  87. }
  88.  
  89. void network_handler(t_menusystem *ms, t_menuitem *mi)
  90. {
  91.   // mi=network since this is handler only for that.
  92.   (void)ms; // Unused
  93.  
  94.   if (mi->data == (void *)none) mi->item = nonet;
  95.   if (mi->data == (void *)stat) mi->item = statnet;
  96.   if (mi->data == (void *)dhcp) mi->item = dhcpnet;
  97. }
  98.  
  99. void checkbox_handler(t_menusystem *ms, t_menuitem *mi)
  100. {
  101.   (void)ms; /* Unused */
  102.  
  103.     if (mi->action != OPT_CHECKBOX) return;
  104.     
  105.     if (strcmp(mi->data,"baseurl") == 0) flags.baseurl = (mi->itemdata.checked ? 1 : 0);
  106.     if (strcmp(mi->data,"winrepair") == 0) {
  107.         if (mi->itemdata.checked)
  108.         {
  109.             flags.winrep = 1;
  110.             linrep->action = OPT_INACTIVE;
  111.         }
  112.         else
  113.         {
  114.             flags.winrep = 0;
  115.             linrep->action = OPT_CHECKBOX;
  116.         }
  117.     }
  118.     if (strcmp(mi->data,"linrepair") == 0) {
  119.         if (mi->itemdata.checked)
  120.         {
  121.             flags.linrep = 1;
  122.             winrep->action = OPT_INACTIVE;
  123.         }
  124.         else
  125.         {
  126.             flags.winrep = 0;
  127.             winrep->action = OPT_CHECKBOX;
  128.         }
  129.     }
  130.     if (strcmp(mi->data,"mountcd") == 0) flags.mountcd = (mi->itemdata.checked ? 1 : 0);
  131. }
  132.  
  133. /*
  134.   Clears keyboard buffer and then 
  135.   wait for stepsize*numsteps milliseconds for user to press any key
  136.   checks for keypress every stepsize milliseconds.
  137.   Returns: 1 if user pressed a key (not read from the buffer),
  138.            0 if time elapsed
  139. */
  140. int checkkeypress(int stepsize, int numsteps)
  141. {
  142.   int i;
  143.   clearkbdbuf();
  144.   for (i=0; i < numsteps; i++)
  145.     {
  146.       if (checkkbdbuf()) return 1;
  147.       sleep(stepsize);
  148.     }
  149.   return 0;
  150. }
  151.  
  152. int menumain(char *cmdline)
  153. {
  154.   t_menuitem * curr;
  155.   char cmd[160];
  156.   char ip[30];
  157.  
  158.   (void)cmdline;        /* Not used */
  159.  
  160.   // Switch video mode here
  161.   // setvideomode(0x18); // or whatever mode you want
  162.  
  163.   // Choose the default title and setup default values for all attributes....
  164.   init_menusystem(NULL);
  165.   set_window_size(1,1,20,78); // Leave some space around
  166.   
  167.   // Choose the default values for all attributes and char's
  168.   // -1 means choose defaults (Actually the next 4 lines are not needed)
  169.   //set_normal_attr (-1,-1,-1,-1); 
  170.   //set_status_info (-1,-1); // Display status on the last line
  171.   //set_title_info  (-1,-1); 
  172.   //set_misc_info(-1,-1,-1,-1);
  173.  
  174.   // Register the menusystem handler
  175.   reg_handler(&msys_handler);
  176.   // Register the ontimeout handler, with a time out of 10 seconds
  177.   reg_ontimeout(ontimeout,1000,0);
  178.  
  179.   NETMENU = add_menu(" Init Network ");
  180.   none = add_item("<N>one","Dont start network",OPT_RADIOITEM,"no ",0);
  181.   dhcp = add_item("<d>hcp","Use DHCP",OPT_RADIOITEM,"dhcp ",0);
  182.   stat = add_item("<s>tatic","Use static IP I will specify later",OPT_RADIOITEM,"static ",0);
  183.  
  184.   TESTING = add_menu(" Testing ");
  185.   set_menu_pos(5,55);
  186.   add_item("<M>emory Test","Perform extensive memory testing",OPT_RUN, "memtest",0);
  187.   add_item("<I>nvisible","You dont see this",OPT_INVISIBLE,"junk",0);
  188.   add_item("<E>xit this menu","Go one level up",OPT_EXITMENU,"exit",0);
  189.  
  190.   RESCUE = add_menu(" Rescue Options ");
  191.   add_item("<L>inux Rescue","linresc",OPT_RUN,"linresc",0);
  192.   add_item("<D>os Rescue","dosresc",OPT_RUN,"dosresc",0);
  193.   add_item("<W>indows Rescue","winresc",OPT_RUN,"winresc",0);
  194.   add_item("<E>xit this menu","Go one level up",OPT_EXITMENU,"exit",0);
  195.  
  196.   PREP = add_menu(" Prep options ");
  197.   baseurl = add_item("<b>aseurl by IP?","Specify gui baseurl by IP address",OPT_CHECKBOX,"baseurl",0);
  198.   mountcd = add_item("<m>ountcd?","Mount the cdrom drive?",OPT_CHECKBOX,"mountcd",0);
  199.   network = add_item(dhcpnet,"How to initialise network device?",OPT_RADIOMENU,NULL,NETMENU);
  200.   add_sep();
  201.   winrep  = add_item("Reinstall <w>indows","Re-install the windows side of a dual boot setup",OPT_CHECKBOX,"winrepair",0);
  202.   linrep  = add_item("Reinstall <l>inux","Re-install the linux side of a dual boot setup",OPT_CHECKBOX,"linrepair",0);
  203.   add_sep();
  204.   runprep = add_item("<R>un prep now","Execute prep with the above options",OPT_RUN,"prep",0);
  205.   add_item("<E>xit this menu","Go up one level",OPT_EXITMENU,"exitmenu",0);
  206.   baseurl->handler = &checkbox_handler;
  207.   mountcd->handler = &checkbox_handler;
  208.   winrep->handler = &checkbox_handler;
  209.   linrep->handler = &checkbox_handler;
  210.   network->handler = &network_handler;
  211.   flags.baseurl = 0;
  212.   flags.mountcd = 0;
  213.   flags.winrep = 0;
  214.   flags.linrep = 0;
  215.  
  216.   MAIN = add_menu(" Main Menu ");  
  217.   add_item("<P>repare","prep",OPT_RUN,"prep",0);
  218.   add_item("<P>rep options...","Options for prep image",OPT_SUBMENU,NULL,PREP);
  219.   add_item("<R>escue options...","Troubleshoot a system",OPT_SUBMENU,NULL,RESCUE);
  220.   add_item("<T>esting...","Options to test hardware",OPT_SUBMENU,NULL,TESTING);
  221.   add_item("<E>xit to prompt", "Exit the menu system", OPT_EXITMENU, "exit", 0);
  222.  
  223.   csprint("Press any key within 5 seconds to show menu...",0x07);
  224.   if (!checkkeypress(100,50)) // Granularity of 100 milliseconds
  225.     {
  226.       csprint("Sorry! Time's up.\r\n",0x07);
  227.       return 1;
  228.     }
  229.   else clearkbdbuf(); // Just in case user pressed something important
  230.   curr = showmenus(MAIN);
  231.   if (curr)
  232.   {
  233.         if (curr->action == OPT_RUN)
  234.         {
  235.             strcpy(cmd,curr->data);
  236.             if (curr == runprep)
  237.             {
  238.                 strcat(cmd,infoline);
  239.         if (network->data == (void *)stat) // We want static
  240.                 {
  241.                     csprint("Enter IP address (last two octets only): ",0x07);
  242.                     getstring(ip, sizeof ip);
  243.                     strcat(cmd,"ipaddr=192.168.");
  244.                     strcat(cmd,ip);
  245.                 }
  246.             }
  247.             if (syslinux)
  248.                runcommand(cmd);
  249.             else csprint(cmd,0x07);
  250.             return 1; // Should not happen when run from SYSLINUX
  251.         }
  252.   }
  253.   // If user quits the menu system, control comes here
  254.   // If you want to execute some specific command uncomment the next two lines
  255.  
  256.   // if (syslinux) runcommand(YOUR_COMMAND_HERE);
  257.   // else csprint(YOUR_COMMAND_HERE,0x07);
  258.  
  259.   // Return to prompt
  260.   return 0;
  261. }
  262.  
  263.